<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue-组件化的组件访问$chindren</title>
</head>

<body>
    <div id="app">
        <cpn></cpn>
        <cpn></cpn>
        <cpn></cpn>
        <cpn></cpn>
        <cpn ref="aaa"></cpn>
        <button @click="btnClick">按&nbsp;钮</button>
    </div>
    <template id="cpn">
        <div>我是子组件</div>
    </template>
    <script src="./js/vue.min.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                message: 'hello'
            },
            methods: {
                btnClick() {
                    // 1.$children
                    // console.log(this.$children);
                    // 可以调用子组件方法
                    // this.$children[0].showMessage();

                    // for (let c of this.$children) {
                    //     console.log(c.name);
                    //     c.showMessage()

                    // }
                    // 开发时一般不会通过$children去拿子组件的数据 
                    // this.$children[0].showMessage();
                    // this.$children[3].name;  通过下标，下标可能会变化，不适用  只有要拿所有子组件时使用

                    // 2.$refs =>对象类型，默认是一个空的对象
                    // console.log(this.$refs);
                    console.log(this.$refs.aaa.name);


                }
            },
            // 注册组件
            components: {
                cpn: {
                    template: '#cpn',
                    data() {
                        return {
                            name: '我是子组件name'
                        }
                    },
                    props: {

                    },
                    methods: {
                        showMessage() {
                            console.log('showMessage');
                        }
                    }

                }
            }

        })
    </script>
</body>

</html>